package com.maimao.goods.controller;

import com.maimao.api.mall.goods.SpecGroupApi;
import com.maimao.core.enums.RespCode;
import com.maimao.core.model.resp.Result;
import com.maimao.goods.controller.base.BaseController;
import com.maimao.goods.service.SpecGroupService;
import com.maimao.model.common.StatusInput;
import com.maimao.model.goods.SpecGroup;
import com.maimao.model.goods.input.SpecOrderInput;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author MaoLin Wang
 * @date 2020/12/11 8:15 下午
 */
@RestController
public class SpecGroupController extends BaseController implements SpecGroupApi {
    @Autowired
    private SpecGroupService specGroupService;

    /**
     * 根据三级分类查询规格组
     */
    @Override
    @GetMapping("/group/by-cate")
    public Result<?> selectGroupByCid3(@RequestParam("cateId") Integer cid3) {
        if (cid3 == null || cid3 < 1) {
            return Result.notValid();
        }
        return Result.okData(specGroupService.selectByCateId(cid3));
    }

    /**
     * 修改规格组排序
     */
    @Override
    @PostMapping("/group/updateOrder")
    public Result<?> moveGroupOrder(@Validated @RequestBody SpecOrderInput input) {
        specGroupService.updateOrder(input);
        return Result.ok(RespCode.OK);
    }

    /**
     * 修改规格组名称
     *
     * @param specGroup
     * @return
     */
    @Override
    @PostMapping("/group/updateName")
    public Result<?> updateGroupName(@RequestBody SpecGroup specGroup) {
        if (specGroup.getId() == null || specGroup.getId() < 1) {
            return Result.notValid();
        }
        if (StringUtils.isEmpty(specGroup.getName())) {
            return Result.ok();
        }
        specGroupService.updateGroupName(specGroup);
        return Result.ok();
    }

    @Override
    @PostMapping("/addGroup")
    public Result<?> addGroup(@Valid @RequestBody SpecGroup specGroup, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return entityInvalid(bindingResult);
        }
        specGroupService.addGroup(specGroup);
        return Result.ok();
    }

    @Override
    @PostMapping("/updateGroup")
    public Result<?> updateGroup(@Valid @RequestBody SpecGroup specGroup, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return entityInvalid(bindingResult);
        }
        if (specGroup.getId() == null || specGroup.getId() < 1) {
            return Result.notValid();
        }
        specGroupService.updateGroup(specGroup);
        return Result.ok();
    }


}
